package com.daquexian.flexiblerichtextview;

import java.util.regex.Pattern;

public class FormatUtil {
    private static String[] OPTIONS = {"  A. ", "  B. ", "  C. ", "  D. ", "  E. ", "  F. ", "  G. ", };
    public static String format_questionNo(String str) {
        return "\r\n" + str + ". ";
    }

    public static String format_question(String str) {
        str = format_p_rnNum(str, 0);
        str = str + "\r\n";
        return str;
    }


    public static String format_option(int index, String str) {
        str = format_p_rnNum(str, 0);
        if(index < OPTIONS.length) {
            str = OPTIONS[index] + str + "\r\n";
        } else {
            str = "**" + str +  "\r\n";
        }
        return str;
    }

    public static String format_option_2(String str) {
        str = format_p_rnNum(str, 0);
        str = format_p_rnNum_all(str, 0);
        return str + " ";
    }

    public static String format_answers(int index, String str, int size) {
        str = format_p_rnNum(str, 0);
        if(index == 0) {
            str = "\r\n  【答案】  " + str;
        }
        if(index == (size - 1)) {
            str = str + "\r\n";
        }
        return str;
    }

    public static String format_explain(String str) {
        str = format_p_rnNum(str, 0);
        str = "  【解析】   " + str + "\r\n";
        return str;
    }

    public static String format_totalQuestion(String str) {
        return "  (总共 " + str + " 题)\r\n";
    }
    public static String format_questionScreen(String str) {
        return "\r\n" + str;
    }
    public static String format_difficulty(String str) {
        str = format_p_rnNum(str, 0);
        str = "  【难度】  " + str + "\r\n";
        return str;
    }
    public static String format_examinationPoints(String str) {
        str = format_p_rnNum(str, 0);
        str = "  【考点】  " + str + "\r\n";
        return str;
    }
    public static String format_score(String str) {
        str = format_p_rnNum(str, 0);
        str = "  【分值】  " + str + "分" + "\r\n";
        return str;
    }

    //设置最后一个</p>标签后面添加几个\r\n
    public static String format_p_rnNum(String str, int rnNum) {
        if(str == null) {
            str = "";
        }
        str = str.trim();
        if(str.length() >= 4) {
            if(str.substring(str.length() - 4).equals("</p>")) {
                str = str.substring(0, str.length() - 4) + "</p rn_num=\"" + rnNum + "\">";
            }
        }
        return str;
    }

    //设置所有的</p>标签后面添加几个\r\n
    public static String format_p_rnNum_all(String str, int rnNum) {
        if(str == null) {
            str = "";
        }
        str = str.trim();
        str = str.replaceAll("</p>", "</p rn_num=\"" + rnNum + "\">");
        return str;
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

}
